package com.example.zg.learn.customView;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 描述：
 * zhougan
 * 2019/1/20
 **/
public class MyAnimView1 extends View {
    private Point customPoint;
    private Paint mPaint;
    private int r=100;

    public MyAnimView1(Context context) {
        super(context);
        initPaint();
    }

    public MyAnimView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public MyAnimView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint(){

        if(mPaint==null){
            mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.RED);
            mPaint.setAntiAlias(true);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i("customViews", "onDraw: ");
        if(customPoint==null){
            customPoint=new Point(r,r);
            drawCicle(canvas);
            startAnim();
        }else{
            drawCicle(canvas);
        }


        super.onDraw(canvas);
    }


    private void drawCicle(Canvas canvas){
        canvas.drawCircle(customPoint.x,customPoint.y,r,mPaint);

    }

    public void startAnim(){
        Point startPoint=new Point(r,r);
        final Point endPoint=new Point(getWidth()-r,getHeight()-r);
        Animator animator= ValueAnimator.ofObject(new ArgbEvaluator(){
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {

                Point startPoint= (Point) startValue;
                Point endPoint= (Point) endValue;
                int x=(int)((endPoint.x-startPoint.x)*fraction+startPoint.x);
                int y=(int)((endPoint.y-startPoint.y)*fraction+startPoint.y);
                customPoint=new Point(x,y);
                Log.i("evaluate", "evaluate: "+customPoint);
                invalidate();
                return customPoint;

            }
        },startPoint,endPoint);

        animator.setDuration(5000);
        animator.start();

    }
}